home *** CD-ROM | disk | FTP | other *** search
- ############################################################################
- #
- # Name: miu.icn
- #
- # Title: Generate strings from the MIU system
- #
- # Author: Cary A. Coutant, modified by Ralph E. Griswold
- #
- # Date: December 27, 1989
- #
- ############################################################################
- #
- # This program generates strings from the MIU string system.
- #
- # The number of generations is determined by the command-line argument.
- # The default is 7.
- #
- # Reference:
- #
- # Godel, Escher, and Bach: an Eternal Golden Braid, Douglas R.
- # Hofstadter, Basic Books, 1979. pp. 33-36.
- #
- ############################################################################
-
- procedure main(arg)
- local count, gen, limit
-
- count := 0
- limit := integer(arg[1]) | 7
- gen := ["MI"]
- every count := 1 to limit do {
- show(count,gen)
- gen := nextgen(gen)
- }
- end
-
- # show - show a generation of strings
-
- procedure show(count,gen)
- write("Generation #",count)
- every write(" ",image(\!gen))
- write()
- end
-
- # nextgen - given a generation of strings, compute the next generation
-
- procedure nextgen(gen)
- local new, s
- new := set()
- every insert(new,apply(!gen))
- return sort(new)
- end
-
- # apply - produce all strings derivable from s in a single rule application
-
- procedure apply(s)
- local i
- if s[-1] == "I" then suspend s || "U"
- if s[1] == "M" then suspend s || s[2:0]
- every i := find("III",s) do
- suspend s[1:i] || "U" || s[i+3:0]
- every i := find("UU",s) do
- suspend s[1:i] || s[i+2:0]
- end
-